package com.bingo.transaction;

import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * 发送同步消息
 */
@Component
public class TxProducer {

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    public void send(String id, String payload) {
        String time = new Date().getTime() + "";
        String uuid = UUID.randomUUID().toString();
        Message<String> message = MessageBuilder.withPayload(payload)
                .setHeader(RocketMQHeaders.KEYS, time)
                .setHeader("uuid", uuid)
                .build();
        System.out.println("time:" + time);
        LocalTxArg localTxArg = LocalTxArg.builder().id(id).build();
        TransactionSendResult sendResult = rocketMQTemplate.sendMessageInTransaction(TxProducerListener.TX_PRODUCER_GROUP, "Base:springboot-tx", message, localTxArg);
        System.out.println(Thread.currentThread().getId() + "---" + Thread.currentThread().getName());
        System.out.println(sendResult);
        System.out.println(MQUtils.get(uuid));
    }
}
